package com.briup.jz.service.impl;

import com.briup.jz.bean.BaseUserRole;
import com.briup.jz.bean.BaseUserRoleExample;
import com.briup.jz.dao.BaseUserRoleMapper;
import com.briup.jz.service.IBaseUserRoleService;
import com.briup.jz.utils.CustomerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BaseUserRoleServiceImpl implements IBaseUserRoleService {

    @Autowired
    private BaseUserRoleMapper baseUserRoleMapper;

    @Override
    public void save(BaseUserRole baseUserRole) throws CustomerException {
        BaseUserRoleExample example = new BaseUserRoleExample();
        BaseUserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(baseUserRole.getUserId());
        criteria.andRoleIdEqualTo(baseUserRole.getRoleId());
        List<BaseUserRole> burs = baseUserRoleMapper.selectByExample(example);
        if(burs.size()>0){
            throw new CustomerException("角色重复添加");
        }
        baseUserRoleMapper.insert(baseUserRole);
    }

    @Override
    public List<BaseUserRole> query() {
        BaseUserRoleExample example = new BaseUserRoleExample();
        return baseUserRoleMapper.selectByExample(example);
    }

    @Override
    public void deleteById(long id) throws CustomerException {
        BaseUserRole baseUserRole = baseUserRoleMapper.selectByPrimaryKey(id);
        if(baseUserRole==null){
            throw new CustomerException("删除的记录不存在");
        }
        baseUserRoleMapper.deleteByPrimaryKey(id);
    }
}
